package grammar;
import lexical.Key;
import lexical.Word;
import java.util.ArrayList;
import java.util.Objects;

public class Lexer {
    private final ArrayList<Word> words;
    private final ArrayList<MyError> errors = new ArrayList<>();
    public int pos = 0;
    public int whileDeep = 0;

    public Lexer(ArrayList<Word> words) {
        this.words = words;
    }

    public void addWhileDeep() {
        whileDeep++;
    }

    public void subWhileDeep() {
        whileDeep--;
    }

    public void addError(MyError error) {
        for (MyError myError : errors) {
            if (Objects.equals(myError.line, error.line)) {
                return;
            }
        }
        errors.add(error);
        System.out.print(error.toString());
    }

    public boolean isWordType(int offset, Key key) {
        if (pos + offset < words.size()) {
            return words.get(pos + offset).key == key;
        } else {
            return false;
        }
    }

    public Word getCurWord() {
        return words.get(pos++);
    }

    public Word getWord(int offset) {
        return words.get(pos + offset);
    }

    public String toStringError() {
        StringBuilder sb = new StringBuilder();
        for (MyError error : errors) {
            sb.append(error.toString());
        }
        String str = sb.toString();
        if (str.length() > 0) {
            return str.substring(0, str.length() - 1);
        } else {
            return "";
        }
    }
}
